package com.mall.utils;

import com.mall.pojo.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.HandlerInterceptor;

public class JwtInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) {
        System.out.println("拦截器被调用，路径：" + request.getRequestURI()); // 调试日志
        // 1. 从Header获取Token
        String token = request.getHeader("Authorization");

        // 2. 统一校验（失败会直接抛ResponseStatusException）
        JwtUtils.validateToken(token);

        return true; // 放行
    }

}